<?php

namespace app\controllers;

use app\models\Ad;
use app\models\AdPosition;
use app\models\History;
use app\models\Link;
use app\models\Page;
use app\models\Post;
use app\models\PostCate;
use app\models\Product;
use app\models\ProductCate;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\ContactForm;

class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
            [
                'class' => 'yii\filters\HttpCache',
//                'only' => ['index'],
                'etagSeed' => function($action, $params){
                    return serialize(date('Y-m-d'));
                }
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                //backColor'=>'#ffffff',//背景颜色
                'maxLength' => 6, //最大显示个数
                'minLength' => 5,//最少显示个数
                'padding' => 5,//间距
                'height'=>50,//高度
                'width' => 130,  //宽度
                'foreColor'=>'#333333',     //字体颜色
                'offset'=>4,        //设置字符偏移量 有效果
                //'controller'=>'login',        //拥有这个动作的controller
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $mailForm = new ContactForm();

        //友情鏈接
        $links = Link::find()->where(['status' => 1])->orderBy(['sort'=>SORT_DESC])->limit(8)->asArray()->all();

        //获取新闻
        $postCate = PostCate::find()->asArray()->all();
        foreach ($postCate as $key => $cate) {
            $postCate[$key]['posts'] = Post::find()->where(['cid'=>$cate['id'], 'is_top'=>0])->limit(5)->asArray()->orderBy(['created_at'=>SORT_DESC])->all();
            $postCate[$key]['top'] = Post::find()->where(['cid'=>$cate['id'], 'is_top'=>1])->orderBy(['updated_at'=>SORT_DESC])->asArray()->one();
        }

        //产品
        $productCate = ProductCate::find()->where(['pid' => 0])->asArray()->all();
        foreach ($productCate as $key => $cate) {
            $productCate[$key]['tui'] = Product::find()->where(['cid'=>$cate['id'], 'is_tui'=>1])->orderBy(['updated_at'=>SORT_DESC])->asArray()->one();
        }

        $adpoition = AdPosition::find()->asArray()->one();
        $ads = Ad::find()->where(['position_id'=>$adpoition['id'], 'status'=>1])->limit($adpoition['num'])->asArray()->all();

//        var_dump($postCate);die;
        return $this->render('index',[
            'mailForm' => $mailForm,
            'links' => $links,
            'postCate' => $postCate,
            'productCate' => $productCate,
            'ads' => $ads
        ]);
    }

    /**
     * Displays contact page.
     *
     * @return Response|string
     */
    public function actionContact()
    {
        $model = new ContactForm();

        return $this->render('contact', [
            'mailForm' => $model,
        ]);
    }

    public function actionSendMail() {
        Yii::$app->response->format = Response::FORMAT_JSON;
        if (!Yii::$app->request->isAjax) {
            return [
                'state' => 0,
                'msg' => '非法请求!'
            ];
        }
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post())) {
            return $model->contact();
        }
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        $info = Page::find()->where(['key'=>'ABOUT'])->one();
        $about = ($info !== null) ? $info->content : '暂无数据';
        //組合歷程數據
        $history = History::find()->orderBy(['time'=>SORT_ASC])->asArray()->all();
        return $this->render('about',[
            'about' => $about,
            'history' => $history
        ]);
    }
}
